package com.cqw.order.controller;

import com.cqw.common.BaseResponse;
import com.cqw.order.domain.Item;
import com.cqw.order.service.ItemService;
import com.cqw.validate.Add;
import com.cqw.validate.Update;
import com.cqw.vo.order.ItemReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-07-08 23:47
 * @Description: 商品分类controller
 */
@RestController
@RequestMapping("/item")
@Slf4j
@Api(value = "商品分类", tags = "商品分类")
public class ItemController {

    @Resource
    private ItemService itemService;


    @GetMapping("/tree")
    @ApiOperation(value = "商品分类树")
    public BaseResponse tree() {
        List<Item> tree = itemService.tree();
        return BaseResponse.success(tree);
    }


    @PostMapping("/create")
    @ApiOperation(value = "新增商品类别")
    public BaseResponse createItem(@RequestBody @Validated(Add.class) ItemReq itemReq) {
        Item item = itemService.createItem(itemReq);
        return BaseResponse.success(item);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改商品类别")
    public BaseResponse updateItem(@RequestBody @Validated(Update.class) ItemReq itemReq) {
        Item item = itemService.updateItem(itemReq);
        return BaseResponse.success(item);
    }


    @GetMapping("/delete")
    @ApiOperation(value = "逻辑删除")
    public BaseResponse deleteItem(@RequestParam(value = "itemId") Long itemId) {
        boolean deleteFlag = itemService.deleteItem(itemId);
        return BaseResponse.success(deleteFlag);
    }


    @GetMapping("/refresh")
    @ApiOperation(value = "刷新缓存")
    public BaseResponse refreshCache(@RequestParam(value = "itemId") Long itemId) {
        itemService.deleteCache(itemId);
        return BaseResponse.success();
    }


}
